package com.kakaxi.mybilibili.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.andexert.library.RippleView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.joooonho.SelectableRoundedImageView;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.bean.BannerItemBean;
import com.kakaxi.mybilibili.bean.LiveItemBean;
import com.kakaxi.mybilibili.bean.PartitionItemBean;
import com.kakaxi.mybilibili.bean.RecyMultiItemBean;
import com.kakaxi.mybilibili.utils.CastUncheckedUtil;
import com.kakaxi.mybilibili.utils.DensityUtil;
import com.kakaxi.mybilibili.utils.SliderLayoutHelper;
import com.kakaxi.mybilibili.viewholder.BannerViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 直播反复列表数据适配器
 * Created by kakaxi on 16/8/26.
 */

public class LiveHomeRecyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /** 滑动广告的类型 **/
    public static final int VIEW_TYPE_BANNER = 1;
    /** 4个图标入口类型 **/
    public static final int VIEW_TYPE_CATEGORY_ICON = 2;
    /** 每种类型的标题 **/
    public static final int VIEW_TYPE_PARTITION = 3;
    /** 每一项内容 **/
    public static final int VIEW_TYPE_CONTENT = 4;

    /** 关注主播分类图标位置常量 **/
    public static final int COLUMN_CONSIDER_CATEGORY_ICON = 0;
    /** 直播中心分类图标位置常量 **/
    public static final int COLUMN_CENTER_CATEGORY_ICON = 1;
    /** 搜索直播分类图标位置常量 **/
    public static final int COLUMN_SEARCH_CATEGORY_ICON = 2;
    /** 全部分类分类图标位置常量 **/
    public static final int COLUMN_ALL_CATEGORY_ICON = 3;

    /** 上下文 **/
    private Context mContext;
    /** 解析布局工具 **/
    private LayoutInflater mInflater;

    /** 数据 **/
    private List<RecyMultiItemBean> mDataList;

    /** 图片加载管理器 **/
    private RequestManager mGlideManager;

    /** 列表点击监听器 **/
    private onRecyclerViewClickListener mListener;


    public LiveHomeRecyAdapter(Context context) {
        this(context, new ArrayList<RecyMultiItemBean>());
    }

    public LiveHomeRecyAdapter(Context context, List<RecyMultiItemBean> dataList) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mGlideManager = Glide.with(context);

        mDataList = dataList;
    }

    /**
     * 数据设置
     *
     * @param dataList 数据
     */
    public void setData(List<RecyMultiItemBean> dataList) {
        mDataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        View view;
        switch (viewType) {
            case VIEW_TYPE_BANNER: { // 广告类型
                view = mInflater.inflate(R.layout.item_recy_common_banner, parent, false);
                viewHolder = new BannerViewHolder(view);
                break;
            }
            case VIEW_TYPE_CATEGORY_ICON: { // 分类文本按钮
                view = mInflater.inflate(R.layout.item_recy_live_category_icon, parent, false);
                viewHolder = new CategoryIconViewHolder(view);
                break;
            }
            case VIEW_TYPE_PARTITION: {
                view = mInflater.inflate(R.layout.item_recy_live_partition, parent, false);
                viewHolder = new PartitionViewHolder(view);
                break;
            }
            case VIEW_TYPE_CONTENT: {
                view = mInflater.inflate(R.layout.item_recy_live_content, parent, false);
                viewHolder = new ContentViewHolder(view);
                break;
            }
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder == null) {
            return;
        }

        RecyMultiItemBean recyMultiItemBean = mDataList.get(position);
        if (recyMultiItemBean != null) {
            // 把信息类设置进视图中
            holder.itemView.setTag(recyMultiItemBean);
            // 获取列表项的类型和数据
            int itemType = recyMultiItemBean.getType();
            final Object itemData = recyMultiItemBean.getData();
            // 根据类型进行不同布局的数据处理和显示
            if (itemType == VIEW_TYPE_BANNER) { // 滑动图片处理
                if (itemData != null) {
                    ArrayList<BannerItemBean> bannerList = CastUncheckedUtil.cast(itemData);
                    BannerViewHolder bannerViewHolder = (BannerViewHolder) holder;
                    // 利用帮助工具设置滑动图片
                    SliderLayoutHelper.setUrlSlider(bannerViewHolder.bannerSliLayout, bannerList,
                            new BaseSliderView.OnSliderClickListener() {
                                @Override
                                public void onSliderClick(BaseSliderView slider) {
                                    if (mListener != null) {
                                        mListener.onBannerItemClick(holder.itemView, slider);
                                    }
                                }
                            });
                }
            } else if (recyMultiItemBean.getType() == VIEW_TYPE_CATEGORY_ICON) {
                CategoryIconViewHolder categoryIconViewHolder = (CategoryIconViewHolder) holder;
                bindCategoryIconViewHolder(categoryIconViewHolder);
            } else if (recyMultiItemBean.getType() == VIEW_TYPE_PARTITION) {
                if (itemData != null) { // 栏目处理
                    PartitionItemBean partitionItemBean = (PartitionItemBean) itemData;
                    PartitionViewHolder partitionViewHolder = (PartitionViewHolder) holder;
                    partitionViewHolder.setData(partitionItemBean);
                    partitionViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mListener != null) {
                                mListener.onPartitionItemClick(v);
                            }
                        }
                    });
                }
            } else if (recyMultiItemBean.getType() == VIEW_TYPE_CONTENT) {
                if (itemData != null) { // 正文处理
                    LiveItemBean liveItemBean = (LiveItemBean) itemData;
                    ContentViewHolder contentViewHolder = (ContentViewHolder) holder;
                    contentViewHolder.setData(liveItemBean);
                }
            }
        }
    }

    /**
     * 绑定分类图标的数据以及监听事件
     *
     * @param viewHolder 视图持久化
     */
    private void bindCategoryIconViewHolder(CategoryIconViewHolder viewHolder) {
        viewHolder.considerTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onCategoryItemClick(v, COLUMN_CONSIDER_CATEGORY_ICON);
                }
            }
        });
        viewHolder.centerTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onCategoryItemClick(v, COLUMN_CENTER_CATEGORY_ICON);
                }
            }
        });
        viewHolder.searchTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onCategoryItemClick(v, COLUMN_SEARCH_CATEGORY_ICON);
                }
            }
        });
        viewHolder.allTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onCategoryItemClick(v, COLUMN_ALL_CATEGORY_ICON);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mDataList != null) {
            return mDataList.size();
        }
        return 0;
    }


    @Override
    public int getItemViewType(int position) {
        if (mDataList != null) {
            return mDataList.get(position).getType();
        }
        return super.getItemViewType(position);
    }

    public onRecyclerViewClickListener getListener() {
        return mListener;
    }

    public void setListener(onRecyclerViewClickListener listener) {
        mListener = listener;
    }

    /**
     * 分类图标的视图绑定器
     */
    private class CategoryIconViewHolder extends RecyclerView.ViewHolder {

        /** 关注主播文本按钮 **/
        TextView considerTxt;
        /** 直播中心文本按钮 **/
        TextView centerTxt;
        /** 搜索直播文本按钮 **/
        TextView searchTxt;
        /** 所有分类文本按钮 **/
        TextView allTxt;

        public CategoryIconViewHolder(View itemView) {
            super(itemView);
            considerTxt = (TextView) itemView.findViewById(R.id.txt_live_home_category_consider);
            centerTxt = (TextView) itemView.findViewById(R.id.txt_live_home_category_center);
            searchTxt = (TextView) itemView.findViewById(R.id.txt_live_home_category_search);
            allTxt = (TextView) itemView.findViewById(R.id.txt_live_home_category_all);

            // 设置图标
            int width = DensityUtil.dipToPx(mContext, 50);
            int height = DensityUtil.dipToPx(mContext, 50);

            Drawable drawable = ActivityCompat.getDrawable(mContext, R.drawable
                    .live_home_follow_anchor);
            drawable.setBounds(0, 0, width, height);
            considerTxt.setCompoundDrawables(null, drawable, null, null);

            drawable = ActivityCompat.getDrawable(mContext, R.drawable.live_home_live_center);
            drawable.setBounds(0, 0, width, height);
            centerTxt.setCompoundDrawables(null, drawable, null, null);

            drawable = ActivityCompat.getDrawable(mContext, R.drawable.live_home_search_room);
            drawable.setBounds(0, 0, width, height);
            searchTxt.setCompoundDrawables(null, drawable, null, null);

            drawable = ActivityCompat.getDrawable(mContext, R.drawable.live_home_all_category);
            drawable.setBounds(0, 0, width, height);
            allTxt.setCompoundDrawables(null, drawable, null, null);

        }
    }

    /**
     * 栏目视图绑定器
     */
    private class PartitionViewHolder extends RecyclerView.ViewHolder {

        /** 栏目图标 **/
        ImageView iconImg;
        /** 标题名称 **/
        TextView nameTxt;
        /** 直播人数 **/
        TextView countTxt;

        public PartitionViewHolder(View itemView) {
            super(itemView);
            iconImg = (ImageView) itemView.findViewById(R.id.img_live_partition_icon);
            nameTxt = (TextView) itemView.findViewById(R.id.txt_live_partition_name);
            countTxt = (TextView) itemView.findViewById(R.id.txt_live_partition_num);
        }

        public void setData(PartitionItemBean partitionItemBean) {
            if (partitionItemBean != null) {
                mGlideManager.load(partitionItemBean.getSubIcon().getSrc()).into(iconImg);
                //Netroid.displayImage(partitionItemBean.getSubIcon().getSrc(), iconImg);
                nameTxt.setText(partitionItemBean.getName());
                String countStr = "当前<font color='#FF0000'>" + partitionItemBean.getCount() +
                        "</font>个直播>";
                countTxt.setText(Html.fromHtml(countStr));
            }
        }

    }

    /**
     * 正文视图绑定器
     */
    private class ContentViewHolder extends RecyclerView.ViewHolder {

        /** 封面 **/
        SelectableRoundedImageView coverSelRouImg;
        /** 标题 **/
        TextView titleTxt;
        /** 观看人数 **/
        TextView onlineTxt;
        /** 发布者的昵称 **/
        TextView ownNameTxt;
        /** 点击效果控件 **/
        RippleView clickRip;

        public ContentViewHolder(View itemView) {
            super(itemView);
            coverSelRouImg = (SelectableRoundedImageView) itemView.findViewById(R.id
                    .srimg_live_content_cover);
            titleTxt = (TextView) itemView.findViewById(R.id.txt_live_content_title);
            onlineTxt = (TextView) itemView.findViewById(R.id.txt_live_content_online);
            ownNameTxt = (TextView) itemView.findViewById(R.id.txt_live_content_own_name);
            clickRip = (RippleView) itemView.findViewById(R.id.rv_live_content_ripple);
        }

        public void setData(final LiveItemBean liveItemBean) {
            if (liveItemBean != null) {
                mGlideManager.load(liveItemBean.getCover().getSrc()).into(coverSelRouImg);
                //Netroid.displayImage(liveItemBean.getCover().getSrc(), coverSelRouImg);
                titleTxt.setText(liveItemBean.getTitle());
                onlineTxt.setText(String.valueOf(liveItemBean.getOnline()));
                ownNameTxt.setText(liveItemBean.getOwner().getName());

                // 监听器设置
                clickRip.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                    @Override
                    public void onComplete(RippleView rippleView) {
                        if (mListener != null) {
                            mListener.onContentItemClick(itemView);
                        }
                    }
                });
            }
        }

    }

    /**
     * 当前循环列表项的点击监听器
     */
    public interface onRecyclerViewClickListener {

        /**
         * 轮播列表项点击事件
         *
         * @param view           带数据的父视图
         * @param baseSliderView 滑动视图
         */
        void onBannerItemClick(View view, BaseSliderView baseSliderView);

        /**
         * 分类按钮列表项点击事件
         *
         * @param view   带数据的父视图
         * @param column 按钮位置
         */
        void onCategoryItemClick(View view, int column);

        /**
         * 标题列表项点击事件
         *
         * @param view 带数据的父视图
         */
        void onPartitionItemClick(View view);

        /**
         * 内容列表项点击事件
         *
         * @param view 带数据的父视图
         */
        void onContentItemClick(View view);

    }


}
